package com.neusoft.neuiotms.dcam.fixed.converter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcam.fixed.dto.StockDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Stock;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.SpareTypeRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.StockRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.StorehouseRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AssetCodeRuleService;
import com.neusoft.neuiotms.dcam.fixed.vo.StockExportVO;
import com.neusoft.neuiotms.dcam.fixed.vo.StockQueryExportVO;

@Component
public class StockConverter extends AbstractConverter<Stock, StockDTO> {

    @Autowired
    private StockRepository stockRepository;

    @Autowired
    private StorehouseRepository storehouseRepository;

    @Autowired
    private ContractRepository contractRepository;

    @Autowired
    private SpareTypeRepository spareTypeRepository;

    @Autowired
    private AssetCodeRuleService assetCodeRuleService;

    @Override
    public Stock toModel(final StockDTO dto) {
        final Stock model;
        if (dto.isNew()) {
            model = new Stock();
        } else {
            model = this.stockRepository.getOne(dto.getId());
        }
        model.setSn(dto.getSn());
        model.setSource(dto.getSource());
        model.setSpecification(dto.getSpecification());
        model.setUnitPrice(dto.getUnitPrice());
        if (StringUtils.isNotEmpty(dto.getStorckNumber())) {
            model.setStorckNumber(dto.getStorckNumber());
        } else {
            model.setStorckNumber(this.assetCodeRuleService.generateAssetCode(dto.getSpareTypeCode(),
                    dto.getStorehouseId().toString(),
                    StringUtils.EMPTY));
        }

        // 设置合同
        if (null != dto.getContractId()) {
            try {
                model.setContract(this.contractRepository.getOne(dto.getContractId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getContractId() });
            }
        }
        //  类型
        if (StringUtils.isNotEmpty(dto.getSpareTypeCode())) {
            try {
                model.setSpareType(this.spareTypeRepository.findByCode(dto.getSpareTypeCode()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getContractId() });
            }
        }
        //        model.setBatch(dto.getBatch());
        //        model.setApplyAssetTypeName(dto.getApplyAssetTypeName());
        //        model.setNumber(dto.getNumber());
        //        model.setUnitPrice(dto.getUnitPrice());
        //
        //        if (null != dto.getGoodsFileId()) {
        //            try {
        //                model.setGoodsFile(this.goodsFileRepository.getOne(dto.getGoodsFileId()));
        //            } catch (final Exception e) {
        //                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getGoodsFileId() });
        //            }
        //        }
        //        if (null != dto.getManufacturerId()) {
        //            try {
        //                model.setManufacturer(this.manufacturerRepository.getOne(dto.getManufacturerId()));
        //            } catch (final Exception e) {
        //                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getManufacturerId() });
        //            }
        //        }
        // 设置仓库
        if (null != dto.getStorehouseId()) {
            try {
                model.setStorehouse(this.storehouseRepository.getOne(dto.getStorehouseId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getStorehouseId() });
            }
        }

        model.setCreateTime(dto.getCreateTime());
        model.setUpdateTime(dto.getUpdateTime());
        return model;
    }

    @Override
    public StockDTO toDTO(final Stock model, final boolean forListView) {
        final StockDTO dto = new StockDTO();

        dto.setId(model.getId());
        dto.setSn(model.getSn());
        dto.setSource(model.getSource());
        dto.setSpecification(model.getSpecification());
        dto.setUnitPrice(model.getUnitPrice());
        dto.setStorckNumber(model.getStorckNumber());
        //        dto.setNumber(model.getNumber());
        //        dto.setUnitPrice(model.getUnitPrice());
        //        dto.setBatch(model.getBatch());
        //
        //        if (model.getGoodsFile() != null) {//货品
        //            dto.setGoodsFileId(model.getGoodsFile().getId());
        //            dto.setGoodsFileName(model.getGoodsFile().getName());
        //            dto.setDescription(model.getGoodsFile().getDescription());
        //            dto.setSpareTypeCode(model.getGoodsFile().getSpareType().getCode());
        //            dto.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
        //            dto.setSpecification(model.getGoodsFile().getSpecification());
        //            dto.setApplyAssetTypeName(model.getGoodsFile().getSpareType().getParent().getName());
        //        }
        //        if (model.getManufacturer() != null) {//厂商
        //            dto.setManufacturerId(model.getManufacturer().getId());
        //            dto.setManufacturerName(model.getManufacturer().getName());
        //        }
        if (model.getStorehouse() != null) {//仓库
            dto.setStorehouseId(model.getStorehouse().getId());
            dto.setStorehouseName(model.getStorehouse().getName());
        }

        if (model.getSpareType() != null) { // 类型
            dto.setSpareTypeName(model.getSpareType().getName());
            dto.setSpareTypeCode(model.getSpareType().getCode());
        }
        if (model.getContract() != null) { // 合同
            dto.setContractId(model.getContract().getId());
            dto.setContractName(model.getContract().getName());
        }
        dto.setStatus(model.getStatus());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

    public StockExportVO toVO(final Stock model) {
        final StockExportVO vo = new StockExportVO();
        //        vo.setNumber(model.getNumber());
        //        vo.setUnitPrice(model.getUnitPrice());
        //        vo.setApplyAssetTypeName(model.getApplyAssetTypeName());
        //        vo.setBatch(model.getBatch());
        //
        //        if (model.getGoodsFile() != null) {//货品
        //            vo.setGoodsFileName(model.getGoodsFile().getName());
        //            vo.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
        //            vo.setSpecification(model.getGoodsFile().getSpecification());
        //            vo.setApplyAssetTypeName(model.getGoodsFile().getSpareType().getParent().getName());
        //        }
        //        if (model.getManufacturer() != null) {//厂商
        //            vo.setManufacturerName(model.getManufacturer().getName());
        //        }
        if (model.getStorehouse() != null) {//库房仓库
            vo.setStorehouseName(model.getStorehouse().getName());
        }

        vo.setStorckNumber(model.getStorckNumber());
        if (model.getSpareType() != null) {
            vo.setSpareTypeName(model.getSpareType().getName());
        }
        vo.setSpecification(model.getSpecification());
        if (model.getContract() != null) { // 合同
            vo.setContractName(model.getContract().getName());
        }
        if (model.getUnitPrice() != null) {
            vo.setUnitPrice(model.getUnitPrice());
        }
        vo.setSn(model.getSn());
        if ("new".equals(model.getSource())) {
            vo.setSource("合同采购");
        } else {
            vo.setSource("退费利旧");
        }

        if ("used".equals(model.getStatus())) {
            vo.setStatus("已使用");
        } else if ("lock".equals(model.getStatus())) {
            vo.setStatus("已分配");
        } else {
            vo.setStatus("闲置");
        }

        return vo;
    }

    public StockQueryExportVO toStockQueryVO(final Stock model) {
        final StockQueryExportVO vo = new StockQueryExportVO();
        //        vo.setNumber(model.getNumber());
        //        vo.setUnitPrice(model.getUnitPrice());
        //        vo.setApplyAssetTypeName(model.getApplyAssetTypeName());
        //        vo.setBatch(model.getBatch());
        //
        //        if (model.getGoodsFile() != null) {//货品
        //            vo.setGoodsFileName(model.getGoodsFile().getName());
        //            vo.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
        //            vo.setSpecification(model.getGoodsFile().getSpecification());
        //            vo.setApplyAssetTypeName(model.getGoodsFile().getSpareType().getParent().getName());
        //            if (model.getGoodsFile().getSpareType() != null) {
        //                vo.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
        //            }
        //        }
        //        if (model.getManufacturer() != null) {//厂商
        //            vo.setManufacturerName(model.getManufacturer().getName());
        //        }
        //        if (model.getStorehouse() != null) {//库房仓库
        //            vo.setStorehouseName(model.getStorehouse().getName());
        //        }

        vo.setCreateTime(model.getCreateTime());
        vo.setUpdateTime(model.getUpdateTime());

        return vo;
    }

}
